<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <p>count:{{count}}</p>
      <p>counter1 count:{{count1}}</p>
      <p>counter2 count:{{count2}}</p>
      <p>counter1 doubleCount1:{{doubleCount1}}</p>
      <p>counter2 doubleCount2:{{doubleCount2}}</p>
      <button @click="add">提交add的mutation</button>
      <button @click="increment">分发increment的action</button>
    </div>
    <script src="vue.js"></script>
    <script src="vuex.js"></script>
    <script>
      Vue.use(Vuex);
      //对于模块内部的 mutation 和 getter，接收的第一个参数是模块的局部状态对象。
       //在模块内部的mutation函数的第1个参数是模块内部的局部状态
      let counter1 = {
        state:{count:1},
        mutations:{
          add(state){//counter1.state
            state.count++;
          }
        },
        actions:{
          increment(context){
            context.commit('add');
          }
        },
        getters:{
          doubleCount1(state){//counter1.state
            return state.count*2;
          }
        }
      }
      let counter2 = {
        state:{count:2},
        mutations:{
          add(state){//counter2.state
            state.count++;
          }
        },
        actions:{
          increment(context){
            context.commit('add');
          }
        },
        getters:{
          doubleCount2(state){//counter1.state
            return state.count*2;
          }
        }
      }
      const store = new Vuex.Store({
        state:{count:0},
        mutations:{
          add(state){
            state.count++;
          }
        },
        actions:{
          increment(context){
            context.commit('add');
          }
        },
        modules:{
          counter1,
          counter2
        }
      });
      var vm = new Vue({
        el:'#app',
        store,
        computed:{
          count(){
            return this.$store.state.count;
          },
          count1(){
            return this.$store.state.counter1.count;
          },
          count2(){
            return this.$store.state.counter2.count;
          },
          doubleCount1(){
            return this.$store.getters.doubleCount1;
          },
          doubleCount2(){
            return this.$store.getters.doubleCount2;
          }
        },
        methods:{
          add(){
            this.$store.commit('add');
          },
          increment(){
            this.$store.dispatch('increment');
          }
        }
      })
      console.log(store.state);//rootState
    </script>
  </body>
</html>
